package com.yds.bluetooth

import android.annotation.SuppressLint
import android.bluetooth.BluetoothDevice
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import com.yds.bluetooth.utils.YDSHexHelper
import com.yds.bluetooth.views.BluetoothDeviceView
import com.yds.bluetooth.views.OnBluetoothFunctionsListener
import com.yds.bluetooth.views.OnBluetoothViewFunctionsListener

/**
 * 低功耗蓝牙
 */
class LowPowerBluetoothActivity : AppCompatActivity(), OnBluetoothFunctionsListener, OnBluetoothViewFunctionsListener {
    private val TAG = "YDS蓝牙"

    private var mRecyclerViewBluetooth: BluetoothDeviceView? = null

    @SuppressLint("ServiceCast", "SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_low_power_bluetooth)

        val init = YDSBluetoothHelper.initBle(
            this,
            "6E400001-B5A3-F393-E0A9-E50E24DCCA9E", "6E400002-B5A3-F393-E0A9-E50E24DCCA9E", "6E400003-B5A3-F393-E0A9-E50E24DCCA9E",
            object : OnConnectStateChangedListener {
                override fun onConnectStateChangedCallback(enable: Boolean) {
                    onConnectStateChanged(enable)
                }
            })
        initView()
    }

    private fun initView() {
        /* 扫描蓝牙设备 */
        findViewById<Button>(R.id.btnSearch).setOnClickListener {
            onScanDeviceStart(true, scanBleOnly = true)
        }

        /* 连接蓝牙设备 */
        findViewById<Button>(R.id.btnConnect).setOnClickListener {
            onConnectToBluetoothDevice { result, device ->
                Log.i(TAG, "连接蓝牙设备: = $result $device")
            }
        }

        /* 发送数据 */
        findViewById<Button>(R.id.btnSend).setOnClickListener {
            val order = "AA 01 30 02 00 01 B0 00 00"
            sendDataToBluetoothDevice(YDSHexHelper.hexToBytes(order), sendResultCallback = {
                Log.i(TAG, "发送数据: = $it")
            }, receiveResultCallback = {
                val result = YDSHexHelper.bytesToHex(it)
                Log.i(TAG, "接收数据: = $result")
            })
        }

        /* 断开连接设备 */
        findViewById<Button>(R.id.btnDisconnect).setOnClickListener {
            onDisconnectFromBluetoothDevice()
        }

        mRecyclerViewBluetooth = findViewById(R.id.listView)
        onBluetoothDeviceDefaultBackground(Color.WHITE)
        onBluetoothDeviceSelectedBackground(Color.parseColor("#ff9999"))
    }

    override fun onScanDeviceStart(autoStop: Boolean, showAnim: Boolean, nameCanEmpty: Boolean, scanBleOnly: Boolean) {
        mRecyclerViewBluetooth?.onScanDeviceStart(autoStop, showAnim, nameCanEmpty, scanBleOnly)
    }

    override fun onScanDeviceStop() {
        mRecyclerViewBluetooth?.onScanDeviceStop()
    }

    override fun onConnectStateChanged(enable: Boolean) {
        mRecyclerViewBluetooth?.onConnectStateChanged(enable)
    }

    override fun onConnectToBluetoothDevice(resultCallback: (Boolean, BluetoothDevice) -> Unit) {
        mRecyclerViewBluetooth?.onConnectToBluetoothDevice(resultCallback)
    }

    override fun sendDataToBluetoothDevice(data: ByteArray, sendResultCallback: (Boolean) -> Unit, receiveResultCallback: (ByteArray) -> Unit) {
        mRecyclerViewBluetooth?.sendDataToBluetoothDevice(data, sendResultCallback, receiveResultCallback)
    }

    override fun onDisconnectFromBluetoothDevice() {
        mRecyclerViewBluetooth?.onDisconnectFromBluetoothDevice()
    }

    override fun onBluetoothDeviceDefaultBackground(colorId: Int) {
        mRecyclerViewBluetooth?.onBluetoothDeviceDefaultBackground(colorId)
    }

    override fun onBluetoothDeviceSelectedBackground(colorId: Int) {
        mRecyclerViewBluetooth?.onBluetoothDeviceSelectedBackground(colorId)
    }

}